package com.in.ussd;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.in.ussd.adapter.SpinnerAdapter;
import com.in.ussd.constant.ConstantVariables;
import com.in.ussd.database.UssdDatasource;
import com.in.ussd.model.UssdAdapterModel;

public class CodeSelectionActivity extends BaseActivity {

	ImageButton btnGo;
	Spinner spnCode;
	UssdDatasource database;
	private List<UssdAdapterModel> listCodes;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_code_selection);

		btnGo = (ImageButton) findViewById(R.id.btnGo);
		spnCode = (Spinner) findViewById(R.id.spnCode);

		btnGo.setOnClickListener(this);

		TextView _disclaimer = (TextView) findViewById(R.id.txtDisclaimer);
		_disclaimer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent i = new Intent(CodeSelectionActivity.this,
						DisclaimerActivity.class);
				startActivity(i);

			}
		});

		database = new UssdDatasource(getApplicationContext());
		setupAdmob();
		share();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		database.open();
		initSpinner();
	}

	private void initSpinner() {

		listCodes = database.getSpinCodesInfo();

		SpinnerAdapter spnStatesAdapter = new SpinnerAdapter(this,
				android.R.layout.simple_spinner_item, listCodes);
		spnStatesAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spnCode.setAdapter(spnStatesAdapter);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		database.close();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.btnGo:
			UssdAdapterModel model = (UssdAdapterModel) spnCode
					.getSelectedItem();
			if (null != model) {
				Intent i = new Intent(CodeSelectionActivity.this,
						UssdInformationActivity.class);
				i.putExtra(ConstantVariables.EXTRA_USSD_ID, model.getUssdId());
				startActivity(i);
				break;
			}
		}
	}
}
